********************************************************************************
*************** MERGING CAB DATA WITH PREPARED COMB DATA WAVE 3 ****************
********************************************************************************
{ /* DO FILE DESCRIPTION
In this do-file the third wave of the CAB and the COMB data sets are merged. In
	the end we will obtain a complete data set that makes it possible to generate
	new variables needed as well as match anthropometric data with household level
	data.

Data sets used:
	statewise cleaned AHS COMB data sets
	statewise cleaned AHS CAB data sets
	
Data sets created:
	multiple temporary versions of the CAB and COMB data
	
	merge_wave3: this data set will be used for data analysis. There will be 
		some more cleaning before then appending it with the DLHS data.	
	
Macros used:
	$temp: folder contains temporary data sets generated in this do file but 
		which are not needed later
	$ final: folder contains final data sets
*/
}
clear
set more off

global final "C:\Users\Michaela\Desktop\manipulated data\final\"
global temp "C:\Users\Michaela\Desktop\manipulated data\temp"



{ /*** Delete non-usual residents from COMB data & de-/tostring variables***/
foreach s of numlist 5 8 9 10 18 20 21 22 23 {
	use "$final\CMW`s'_w3" , clear


* destring vatriables
	# delimit ;
	foreach v in relation_to_head social_group_code occupation_status healthscheme_1
					healthscheme_2 housestatus householdstatus isheadchanged
					illness_type sought_medical_care hh_serial_no { ;
	# delimit cr
			destring `v', replace
	}
* tostring variables
	# delimit ;
		foreach v in disability_status injury_treatment_type treatment_source 
					symptoms_pertaining_illness diagnosis_source regular_treatment_source
					chew smoke alcohol { ;
	# delimit cr
	
			tostring `v', replace
		}

	if `s' == 23 {
		replace hh_serial_no = "" if hh_serial_no == "**"
		destring hh_serial_no, replace
	}

/*** Clean variables that will be used for merging ***/
	
		di "state `s'"
		rename (marital_status psu_id stratum_code house_no member_identity hh_serial_no) ///
			(marital_status_comb psu_id_comb stratum ahs_house_unit identification_code sl_no)
		destring identification_code sl_no, replace
		tab sex, m
		destring sex, replace
		gen byear = year_of_birth
		drop if district == . | stratum == . | ahs_house_unit == . |  house_hold_no == . | sl_no == . | identification_code == .| age == . | sex == . | byear == .
		sort district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear age
		duplicates tag district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear, gen(dup)
		tab dup
		drop if dup > 0
		isid district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear
		sort district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear
		drop dup
	save "$temp\COMB`s'_formerging", replace
}
}
{ /*** Clean and prepare CAB data for merge using unique identifiers ***/
foreach s of numlist 5 8 9 10 18 20 21 22 23  {
	use "$temp\CAB_state`s'.dta", clear
		drop if usual_residance == 2
		destring identification_code, replace
		rename district_code district
		drop if district==. | stratum==. | ahs_house_unit ==. |  house_hold_no==. | sl_no ==. | identification_code ==.|age==.|sex==.| byear==.
		sort district stratum ahs_house_unit house_hold_no sl_no identification_code  sex byear age
		duplicates tag district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear, gen(dup)
		tab dup
		drop if dup > 0
		drop dup
		isid district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear
		sort district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear
	save "$temp\CAB`s'_formerging", replace
} 
}


{ /*** Merge using both cleaned files ***/
foreach s of numlist 5 8 9 10 18 20 21 22 23  {
	use "$temp\CAB`s'_formerging", clear
		di "state `s'"
		merge 1:1 district stratum ahs_house_unit house_hold_no sl_no identification_code sex byear using "$temp\COMB`s'_formerging"
		drop if _merge < 3
		drop _merge

		# delimit ;
		foreach v in client_hh_id hl_id father_serial_no mother_serial_no date_of_marriage 
				date_of_marriage month_of_marriage year_of_marriage currently_attending_school
				currently_attending_school reason_for_not_attending_school is_water_filter
				water_filteration hl_expall_status isdeadmigrated  residancial_status 
				iscoveredbyhealthscheme fid fidh rtelephoneno  isnewrecord recordupdatedcount 
				recordstatus occupation_status sought_medical_care { ;
		# delimit cr	
			destring `v', replace
		}
		
		tostring as_binned, replace

	save "$temp\merge`s'_wave3", replace
}


{ /*** Append merged data sets ***/
use "$temp\merge5_wave3"

	foreach s of numlist 8 9 10 18 20 21 22 23 {
		append using "$temp\merge`s'_wave3"
	}
	
save "$final\merge_wave3", replace
}
}
